//
//  SettingContentVC.m
//  WanCaiC8
//
//  Created by tangang on 2018/3/28.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "SettingContentVC.h"
#import "MyVM.h"
#import "ForgetPasswordVC.h"
@interface SettingContentVC ()<UITextFieldDelegate>

@end

@implementation SettingContentVC{
    UITextField *textF;
    UITextField *oldPwd;
    UITextField *newPwd;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=DEFAULTBGCOLOR;
    
    [self createUI];
    
    // Do any additional setup after loading the view.
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

- (void)backClick{
    NSString *value=@"";
    if (self.type==SettingTypeNickName) {
        if (textF.text.length>=8) {
            [SVProgressHUD showImage:nil status:@"昵称最多7个字"];
            return;
        }
        if (textF.text.length==0){
            [SVProgressHUD showImage:nil status:@"昵称不能为空"];
            return;
        }
        [UserInfoModel share].NickName=textF.text;
        value=textF.text;
    }else if (self.type==SettingTypeSign){
        [UserInfoModel share].Autograph=textF.text;
        value=textF.text;
    }else if (self.type==SettingTypeSex){
        [UserInfoModel share].Sex=self.sex;
        value=[NSString stringWithFormat:@"%ld",self.sex];
    }
    if (value.length>0) {
        WS(weakSelf);
        [[MyVM manage] updateUserInfoWithValue:value andType:self.type+1 andSuccess:^(id returnValue) {
            [SVProgressHUD showSuccessWithStatus:@"修改成功"];
            if (weakSelf.backBlock) {
                weakSelf.backBlock(value);
            }
        } andFail:^(NSString *errorMessage, NSError *error) {
            
        }];
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}


#define TextTag 12
- (void)createUI{
    if (self.type==SettingTypeSign||self.type==SettingTypeNickName) {
        UIView *contentView=[[UIView alloc]init];
        contentView.backgroundColor=[UIColor whiteColor];
        [self.view addSubview:contentView];
        [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.view).with.offset(5);
            make.height.mas_equalTo(@50);
        }];
        textF=[[UITextField alloc]init];
        textF.textColor=DEFAULTTEXTBLACKCOLOR;
        textF.font=[UIFont systemFontOfSize:16];
        if (self.type==SettingTypeSign) {
            textF.placeholder=@"请输入个性签名";
        }else{
            textF.placeholder=@"请输入昵称";
        }
        textF.text=self.strContent;
        textF.clearButtonMode=UITextFieldViewModeWhileEditing;
        [contentView addSubview:textF];
        [textF mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(contentView).with.insets(UIEdgeInsetsMake(0, 10, 0, 10));
        }];
        
    }else if (self.type==SettingTypeSex){
        UIView  *manView=[self sexViewWithTitle:@"男"];
        manView.tag=10;
        [self.view addSubview:manView];
        [manView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.view).with.offset(5);
            make.height.mas_equalTo(@50);
        }];
        
        UIView  *woManView=[self sexViewWithTitle:@"女"];
        woManView.tag=11;
        [self.view addSubview:woManView];
        [woManView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(manView.mas_bottom).with.offset(1);
            make.height.mas_equalTo(@50);
        }];
        [self changeSex];
    }else if (self.type==SettingTypePassword){
        
        QXLable *labOld=[[QXLable alloc]initWithFount:15];
        labOld.text=@"旧密码";
        [self.view addSubview:labOld];
        [labOld mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).with.offset(10);
            make.top.equalTo(self.view);
            make.size.mas_equalTo(CGSizeMake(100, 35));
        }];
        
        UIView *oldView=[[UIView alloc]init];
        oldView.backgroundColor=[UIColor whiteColor];
        [self.view addSubview:oldView];
        [oldView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(labOld.mas_bottom);
            make.height.mas_equalTo(@50);
        }];
        oldPwd=[[UITextField alloc]init];
        oldPwd.textColor=DEFAULTTEXTBLACKCOLOR;
        oldPwd.font=[UIFont systemFontOfSize:15];
        oldPwd.placeholder=@"请输入旧密码";
        oldPwd.keyboardType=UIKeyboardTypeNumbersAndPunctuation;
        oldPwd.clearButtonMode=UITextFieldViewModeWhileEditing;
        oldPwd.secureTextEntry=YES;
        oldPwd.delegate=self;
        [oldView addSubview:oldPwd];
        [oldPwd mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(oldView).with.insets(UIEdgeInsetsMake(0, 10, 0, 10));
        }];
        
        QXLable *labNew=[[QXLable alloc]initWithFount:15];
        labNew.text=@"新密码";
        [self.view addSubview:labNew];
        [labNew mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).with.offset(10);
            make.top.equalTo(oldView.mas_bottom).with.offset(10);
            make.size.mas_equalTo(CGSizeMake(100, 35));
        }];
        UIView *newView=[[UIView alloc]init];
        newView.backgroundColor=[UIColor whiteColor];
        [self.view addSubview:newView];
        [newView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(labNew.mas_bottom).with.offset(1);
            make.height.mas_equalTo(@50);
        }];
        newPwd=[[UITextField alloc]init];
        newPwd.textColor=DEFAULTTEXTBLACKCOLOR;
        newPwd.font=[UIFont systemFontOfSize:15];
        newPwd.placeholder=@"请输入新密码(6-16位)";
        newPwd.keyboardType=UIKeyboardTypeNumbersAndPunctuation;
        newPwd.clearButtonMode=UITextFieldViewModeWhileEditing;
        newPwd.secureTextEntry=YES;
        newPwd.delegate=self;
        [newView addSubview:newPwd];
        [newPwd mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(newView).with.insets(UIEdgeInsetsMake(0, 10, 0, 10));
        }];
        
        UIButton *btnShow =[[UIButton alloc]init];
        [btnShow setImage:[UIImage imageNamed:@"card_noSelected"] forState:UIControlStateNormal];
        [btnShow setImage:[UIImage imageNamed:@"card_selected"] forState:UIControlStateSelected];
        [btnShow setTitle:@"  显示密码" forState:UIControlStateNormal];
        [btnShow setTitleColor:DEFAULTTEXTGDARKRAYCOLOR forState:UIControlStateNormal];
        [btnShow addTarget:self action:@selector(onclickShowPassword:) forControlEvents:UIControlEventTouchUpInside]; btnShow.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
        [btnShow.titleLabel setFont:[UIFont systemFontOfSize:13]];
        [self.view addSubview:btnShow];
        [btnShow mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).with.offset(10);
            make.top.equalTo(newView.mas_bottom);
            make.size.mas_equalTo(CGSizeMake(100, 35));
        }];
        
        
        UIButton *btnForget=[[UIButton alloc]init];
        [btnForget setTitle:@"  忘记密码" forState:UIControlStateNormal];
        [btnForget setTitleColor:DEFAULTTEXTGDARKRAYCOLOR forState:UIControlStateNormal];
        [btnForget setTitleColor:NAVCOLOR forState:UIControlStateNormal];
        [btnForget addTarget:self action:@selector(onclickForgetPassword:) forControlEvents:UIControlEventTouchUpInside]; btnForget.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
        [btnForget.titleLabel setFont:[UIFont systemFontOfSize:13]];
        [self.view addSubview:btnForget];
        [btnForget mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.view).with.offset(-10);
            make.top.equalTo(newView.mas_bottom);
            make.size.mas_equalTo(CGSizeMake(100, 35));
        }];
        
        [self showRightBtnWithUse:NO];
    }
}

- (void)showRightBtnWithUse:(BOOL)use{
    UIButton *btn=[[UIButton alloc]init];
    [btn setTitle:@"完成" forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    if (use) {
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }else{
        [btn setTitleColor:DEFAULTTEXTGRAYCOLOR forState:UIControlStateNormal];
    }
    [btn addTarget:self action:@selector(onclickSure:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightItem=[[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem=rightItem
    ;
    
}

- (void)onclickShowPassword:(UIButton *)sender{
    sender.selected=!sender.isSelected;
    oldPwd.secureTextEntry=!sender.isSelected;
    newPwd.secureTextEntry=!sender.isSelected;
}

- (void)onclickForgetPassword:(UIButton *)sender{
    ForgetPasswordVC *vc=[[UIStoryboard storyboardWithName:@"CLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"ForgetPasswordVC"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)onclickSure:(UIButton *)sender{
    [[MyVM manage] updatePasswordWithOldPwd:oldPwd.text andNewPwd:newPwd.text andSuccess:^(id returnValue) {
        [[BaseVM manage] login];
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
}

#define ImageTag    15
- (UIView *)sexViewWithTitle:(NSString *)title{
    UIView  *sexView=[[UIView alloc]init];
    sexView.backgroundColor=[UIColor whiteColor];
    QXLable *labSex=[[QXLable alloc]initTitle];
    labSex.text=title;
    [sexView addSubview:labSex];
    [labSex mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(sexView).with.offset(10);
        make.centerY.equalTo(sexView);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    
    UIImageView *selectImage=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"sex_select"]];
    selectImage.tag=ImageTag;
    [sexView addSubview:selectImage];
    [selectImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(sexView).with.offset(-15);
        make.centerY.equalTo(sexView);
        make.size.mas_equalTo(selectImage.image.size);
    }];
    UITapGestureRecognizer *tag=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onclickSex:)];
    [sexView addGestureRecognizer:tag];
    return sexView;
}

- (void)onclickSex:(UITapGestureRecognizer *)tap{
    self.sex=tap.view.tag-10;
    [self changeSex];
}


- (void)changeSex{
    UIView *manView=[self.view viewWithTag:10];
    UIImageView *manImage=[manView viewWithTag:ImageTag];
    UIView *woManView=[self.view viewWithTag:11];
    UIImageView *woManImage=[woManView viewWithTag:ImageTag];
    if (self.sex==0) {
        manImage.hidden=NO;
        woManImage.hidden=YES;
    }else{
        woManImage.hidden=NO;
        manImage.hidden=YES;
    }
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (oldPwd.text.length>0&&newPwd.text.length==5) {
        [self showRightBtnWithUse:YES];
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
